import redis

class IdGenerator:
    def __init__(self,client,key):
        self.client = client
        self.key = key

    def prodece_id(self):
        # 生成id
        return self.client.incr(self.key)
    def get_last_id(self):
        # 获取最后一个id
        return self.client.get(self.key)

    def reserve(self,n):
        # 预占id
        result = self.client.set(self.key,n,nx=True)
        return result is True


if __name__ == "__main__":
    r = redis.StrictRedis(host="127.0.0.1", port=6379, db=0, decode_responses=True)
    id_generator = IdGenerator(r,"user::id")
    id_generator.reserve(10000)
    id_generator.prodece_id()
    id_generator.prodece_id()
    print(id_generator.get_last_id())

